package cn.HuanG.pet.controller;

import cn.HuanG.org.domain.Employee;
import cn.HuanG.org.service.IEmployeeService;
import cn.HuanG.pet.domain.Pet;
import cn.HuanG.pet.query.PetQuery;
import cn.HuanG.pet.service.IPetService;
import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.LoginContext;
import cn.HuanG.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;
    @Autowired
    private IEmployeeService employeeService;
    @PostMapping
    public PageInfo<Pet> pet(@RequestBody PetQuery query) {
        PageInfo<Pet> queryPage = petService.queryPage(query);
        return queryPage;
    }
    @PostMapping("/front")
    public PageInfo<Pet> front(@RequestBody PetQuery query) {
        PageInfo<Pet> queryPage = petService.queryPage(query);
        return queryPage;
    }
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        System.out.println("哈哈哈哈哈"+pet);
        System.out.println("++++++++" + pet.getResources());
        try {
            if (pet.getId()==null){
                //获取当前登录用户，再获取对应的点铺
                Employee employee = (Employee)LoginContext.getCurrent(request);
                //获取employee中的shop_id
                Long shop_id = pet.getShop_id();
                pet.setShop_id(shop_id);
                petService.save(pet);
            }else {
                petService.update(pet);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
    @PatchMapping("/onSale")
    public AjaxResult saveOrUpdate(@RequestBody Long[] ids){
        System.out.println("ids数据"+ids);
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody Long[] ids){
        System.out.println("ids数据"+ids);
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @GetMapping("/detail/{id}")
    public Pet detail(@PathVariable Long id){
        Pet pet = petService.getDetail(id);
        return  pet;
    }

}
